<?php

namespace App\Admin\Controllers;

use App\Models\Post as Blog;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;
class BlogController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Blog';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Blog);

        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('cate_id', __('Cate id'));
        $grid->column('title', __('Title'));
        $grid->column('desc', __('Desc'));
        $grid->column('content', __('Content'));
        $grid->column('parse', __('Parse'));
        $grid->column('image', __('Image'));
        $grid->column('pos', __('Pos'));
        $grid->column('group', __('Group'));
        $grid->column('status', __('Status'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Blog::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('cate_id', __('Cate id'));
        $show->field('title', __('Title'));
        $show->field('desc', __('Desc'));
        $show->field('content', __('Content'));
        $show->field('parse', __('Parse'));
        $show->field('image', __('Image'));
        $show->field('pos', __('Pos'));
        $show->field('group', __('Group'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Blog);

        $fields = [
            'user_id' =>[
                'number'=>'用户ID',
            ],
            'title' =>[
                'text'=>'标题',
                'rules' =>'required',
                'default'=>'aaaaa'
            ],            
        ];
        foreach ($fields as $field => $value) {
            $i=0;
            $has = 0;
            foreach ($value as $k => $v) {
                if($i===0){
                    $v = str2arr($v);
                    if ($className = Form::findFieldClass($k)) {
                        $element = new $className($field, $v);
                        $has = 1;
                    }else{
                        continue;
                    }
                }else{
                    $element = $element->$k($v);
                }
                $i++;
            }
            if($has)  $form->pushField($element);
        }
/*
        $form->number('user_id', __('User id'));
        $form->number('cate_id', __('Cate id'));
        $form->text('title', __('Title'));
        $form->text('desc', __('Desc'));
        $form->textarea('content', __('Content'));
        $form->text('parse', __('Parse'));
        $form->image('image', __('Image'));
        $form->text('pos', __('Pos'));
        $form->text('group', __('Group'));
        $form->switch('status', __('Status'))->default(1);
*/
        return $form;
    }
}
